/*
 * Created by JFormDesigner on Sun Mar 27 02:11:19 CST 2022
 */

package com.wangcanxuan.www.view.user;

import com.wangcanxuan.www.controller.Impl.UserControllerImpl;
import com.wangcanxuan.www.controller.UserController;
import com.wangcanxuan.www.entity.dto.ResultInfo;
import com.wangcanxuan.www.entity.po.User;
import com.wangcanxuan.www.proxy.ControllerProxy;
import com.wangcanxuan.www.util.ValidateUtil;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import static com.wangcanxuan.www.entity.dto.ResultConstants.*;

/**
 * 更新用户信息的界面
 *
 * @author unknown
 * @date 2022/04/01
 */
public class UpdateUserMessageView extends JFrame {
    private final User user;
    private final UserController userController;

    public UpdateUserMessageView(User user) {
        userController = ControllerProxy.getProxyInstance(UserControllerImpl.class);
        this.user = user;
        initComponents();
        setVisible(true);
    }

    private void update(ActionEvent e) {
        // TODO add your code here
        String nickName = nickNameText.getText();
        String password = new String(passwordText.getPassword());
        String currentPassword = new String(currentPasswordText.getPassword());
        String phone = phoneText.getText();
        String emailAddress = emailAddressText.getText();
        if (ValidateUtil.strIsNullOrEmpty(nickName)) {
            JOptionPane.showMessageDialog(null, "昵称不能为空");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(password)) {
            JOptionPane.showMessageDialog(null, "密码不能为空！");
            return;
        }
        if (ValidateUtil.isNotPassword(password)) {
            JOptionPane.showMessageDialog(null, "密码格式错误！");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(currentPassword)) {
            JOptionPane.showMessageDialog(null, "确认密码不能为空！");
            return;
        }
        if (!password.equals(currentPassword)) {
            JOptionPane.showMessageDialog(null, "请确认密码是否一致！");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(phone)) {
            JOptionPane.showMessageDialog(null, "手机号码不能为空！");
            return;
        }
        if (ValidateUtil.isNotMobile(phone)) {
            JOptionPane.showMessageDialog(null, "手机号码格式错误");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(emailAddress)) {
            JOptionPane.showMessageDialog(null, "邮箱地址不能为空！");
            return;
        }
        if (ValidateUtil.isNotEmail(emailAddress)) {
            JOptionPane.showMessageDialog(null, "邮箱地址格式错误");
            return;
        }
        user.setNickName(nickName);
        user.setPassword(password);
        user.setPhone(Long.valueOf(phone));
        user.setEmailAddress(emailAddress);
        ResultInfo<?> updateUserMessage = userController.updateUserMessage(user);
        int code = updateUserMessage.getCode();
        String message = updateUserMessage.getMessage();
        if (code == SUCCESS_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            ResultInfo<User> refresh = userController.refresh(user.getId());
            new MainView(refresh.getObj());
            dispose();
        }
        if (code == REJECT_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
        }
    }

    private void back(ActionEvent e) {
        // TODO add your code here
        new MainView(user);
        dispose();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        nickNameText = new JTextField();
        passwordText = new JPasswordField();
        updateButton = new JButton();
        backButton = new JButton();
        label4 = new JLabel();
        currentPasswordText = new JPasswordField();
        label5 = new JLabel();
        phoneText = new JTextField();
        label6 = new JLabel();
        emailAddressText = new JTextField();

        //======== this ========
        setTitle("\u66f4\u65b0\u60a8\u7684\u4fe1\u606f");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u4fee\u6539\u4e2a\u4eba\u4fe1\u606f");
        label1.setEnabled(false);
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
        contentPane.add(label1);
        label1.setBounds(134, 16, 154, 48);

        //---- label2 ----
        label2.setText("\u6635 \u79f0\uff1a");
        contentPane.add(label2);
        label2.setBounds(56, 90, 60, 28);

        //---- label3 ----
        label3.setText("\u5bc6 \u7801\uff1a");
        contentPane.add(label3);
        label3.setBounds(56, 136, 60, 25);
        contentPane.add(nickNameText);
        nickNameText.setBounds(134, 91, 154, nickNameText.getPreferredSize().height);
        contentPane.add(passwordText);
        passwordText.setBounds(134, 137, 154, passwordText.getPreferredSize().height);

        //---- updateButton ----
        updateButton.setText("\u4fee\u6539");
        updateButton.addActionListener(e -> update(e));
        contentPane.add(updateButton);
        updateButton.setBounds(119, 388, 125, updateButton.getPreferredSize().height);

        //---- backButton ----
        backButton.setText("\u8fd4\u56de");
        backButton.addActionListener(e -> back(e));
        contentPane.add(backButton);
        backButton.setBounds(new Rectangle(new Point(24, 16), backButton.getPreferredSize()));

        //---- label4 ----
        label4.setText("\u786e\u8ba4\u5bc6\u7801\uff1a");
        contentPane.add(label4);
        label4.setBounds(56, 180, label4.getPreferredSize().width, 28);
        contentPane.add(currentPasswordText);
        currentPasswordText.setBounds(134, 183, 154, currentPasswordText.getPreferredSize().height);

        //---- label5 ----
        label5.setText("\u624b\u673a\u53f7\uff1a");
        contentPane.add(label5);
        label5.setBounds(56, 226, 60, 27);
        contentPane.add(phoneText);
        phoneText.setBounds(134, 226, 154, phoneText.getPreferredSize().height);

        //---- label6 ----
        label6.setText("\u90ae\u7bb1\u5730\u5740\uff1a");
        contentPane.add(label6);
        label6.setBounds(56, 271, label6.getPreferredSize().width, 29);
        contentPane.add(emailAddressText);
        emailAddressText.setBounds(134, 272, 154, emailAddressText.getPreferredSize().height);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for (int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField nickNameText;
    private JPasswordField passwordText;
    private JButton updateButton;
    private JButton backButton;
    private JLabel label4;
    private JPasswordField currentPasswordText;
    private JLabel label5;
    private JTextField phoneText;
    private JLabel label6;
    private JTextField emailAddressText;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
